package iu;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

import so.InterfaceComServidor;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Iterator;

import jogoBolaoeArmazenador.rn.Usuario;

@SuppressWarnings("serial")
public class TelaInicial extends JFrame {

	private JPanel contentPane;
	private JFrame _essaTela = this;
	private Usuario _oUsuario;
	private JTextArea atualizacoes;

	/**
	 * Create the frame.
	 */
	public TelaInicial(Usuario user) {
		
		_oUsuario = user;
		
		setTitle("Tela Principal");
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 560, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel();
		label.setText("Sena OnLine");
		label.setFont(new Font("Tahoma", Font.PLAIN, 36));
		label.setBounds(184, 11, 199, 44);
		contentPane.add(label);
		
		JSeparator separator = new JSeparator();
		separator.setOrientation(SwingConstants.VERTICAL);
		separator.setBounds(270, 66, 15, 295);
		contentPane.add(separator);
		
		JButton botaoJogarBolao = new JButton();
		botaoJogarBolao.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new jogoBolao.so.InterfaceProvida().iniciarSistema(_essaTela, _oUsuario);
			}
		});
		botaoJogarBolao.setText("Bol\u00E3o");
		botaoJogarBolao.setFont(new Font("Tahoma", Font.PLAIN, 14));
		botaoJogarBolao.setBounds(51, 185, 153, 25);
		contentPane.add(botaoJogarBolao);
		
		JButton botaoJogarIndividual = new JButton();
		botaoJogarIndividual.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				new jogoIndividual.so.InterfaceProvida().iniciarSistema(_essaTela);
			}
		});
		botaoJogarIndividual.setText("Jogo Individual");
		botaoJogarIndividual.setFont(new Font("Tahoma", Font.PLAIN, 14));
		botaoJogarIndividual.setBounds(51, 124, 153, 25);
		contentPane.add(botaoJogarIndividual);
		
		JButton botaoSair = new JButton();
		botaoSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		botaoSair.setText("Sair");
		botaoSair.setFont(new Font("Tahoma", Font.PLAIN, 14));
		botaoSair.setBounds(51, 249, 153, 25);
		contentPane.add(botaoSair);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(295, 95, 238, 254);
		contentPane.add(scrollPane);
		
		atualizacoes = new JTextArea();
		atualizacoes.setEditable(false);
		scrollPane.setViewportView(atualizacoes);
		preencheAtualizacoes();
		
		JLabel lblDesdeOSeu = new JLabel("Desde o seu \u00FAltimo login:");
		lblDesdeOSeu.setFont(new Font("Tahoma", Font.PLAIN, 12));
		lblDesdeOSeu.setBounds(287, 66, 138, 14);
		contentPane.add(lblDesdeOSeu);
		
		setLocationRelativeTo(null);
	}

	private void preencheAtualizacoes() {
		ArrayList<String> informacoes = new InterfaceComServidor().getInformacaoTelaInicial(_oUsuario);
		Iterator<String> i = informacoes.iterator();
		while(i.hasNext()){
			atualizacoes.append(i.next()+"\n");
		}
	}

	public void setMensagem(String mensagem) {
		atualizacoes.setText(mensagem);
	}
}
